<?php
/**
 * @ClassName: DoAgentLevel
 * @Description: 代理等级
 * @author: gydtrade
 * @Date: 2025-11-07 10:39
 * @Version: V1.0.0
 */

namespace app\common\rebate;

use app\common\enum\OrderStatus;
use app\common\enum\OrderType;
use app\common\enum\PayStatus;
use app\common\enum\ShippingStatus;
use app\common\model;

class DoAgentLevel extends HandleLogic
{
    function upAgentLevel($params)
    {
        //方式一(买水）
        $this->upAgentLevelByWater($params);
        //方式二(买水架）
        $this->upAgentLevelByShelf($params);
        //方式三(买礼包）
        $this->upAgentLevelByGiftPack($params);
    }

    protected function upAgentLevelByWater($params)
    {
        //获取订单信息
        $this->getOrderByOrderSn($params['order_sn']);

        if (!$this->order_sn || !$this->orderInfo) {
            makelog($this->order_sn . '订单不存在', 'agent/level/upgrade/water');
            return json(['code' => 0, 'msg' => '订单不存在']);
        }

        // 1.现获取下单人的上级
        $buyer = $this->getBuyer($this->orderInfo);
    }

    protected function upAgentLevelByShelf($params)
    {
        //获取订单信息
        $this->getOrderByOrderSn($params['order_sn']);

        if (!$this->order_sn || !$this->orderInfo) {
            makelog($this->order_sn . '订单不存在', 'agent/level/upgrade/shelf');
            return json(['code' => 0, 'msg' => '订单不存在']);
        }
    }

    protected function upAgentLevelByGiftPack($params)
    {
        //获取订单信息
        $this->getOrderByOrderSn($params['order_sn']);

        if (!$this->order_sn || !$this->orderInfo) {
            makelog($this->order_sn . '订单不存在', 'agent/level/upgrade/giftPack');
            return json(['code' => 0, 'msg' => '订单不存在']);
        }
    }

    protected function upAgentLevelByCondition($member, $type){
        switch ($type){
            case 1:
                // 方式一(买水）
                $deliveryNum = $this->getDeliveryNum($member);//送达桶数

                break;
            case 2:
                // 方式二(买水架）
                break;
            case 3:
                // 方式三(买礼包）
                break;
            default:
                // 异常
                break;
        }
    }

    /**
     * 获取订水送达桶数
     * @param $member
     * @return array
     */
    protected function getDeliveryNum($member)
    {
        $member_id_list = model\Member::where('relation', 'like', ',' . $member["id"] . ',')->field('id')->select();
        $member_ids = [];
        foreach ($member_id_list as $item) {
            $member_ids[] = $item['id'];
        }

        $where = [];
        $where[] = ['o.member_id', 'in', $member_ids];
        $where[] = ['o.order_type', '=', OrderType::water];//订水订单
        $where[] = ['o.order_tag', '=', 'bucket_water'];//订单标识
        $where[] = ['o.pay_status', '=', PayStatus::pay];//已支付
        $where[] = ['o.shipping_status', '=', shippingStatus::shipped];//已发货
        $where[] = ['o.order_status', 'in', [OrderStatus::WAITCCOMMENT, OrderStatus::FINISH]];//已收货/已完成

        $num = model\ShopOrder::alias('o')->leftJoin('shop_order_goods og', 'og.order_id=o.id')->where($where)->count('og.total_num');

        return ["code" => 1, "msg" => "统计订水送达桶数成功", "data" => $num];
    }

    /**
     * 获取直推代理数量
     * @param $member
     * @param $level
     * @return array
     */
    protected function getAgentZhitui($member, $level)
    {
        $where = [];
        $where[] = ["agent_level", "=", $level];
        $where[] = ["first_leader", "=", $member["id"]];
        $count = model\Member::where($where)->count();
        return ["code" => 1, "msg" => "统计直推代理数量成功", "data" => $count];
    }

}